package com.sunhao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sunhao.myapplication.utils.DateUtil;

public class MainActivity7 extends AppCompatActivity  {
    private View tv_result;
    //    control alt f 创建全局变量




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        tv_result = findViewById(R.id.tv_result);

        Button button_standard = findViewById(R.id.btn_standard);
        button_standard.setOnClickListener(new MyClickListener(tv_result));

        //通过构造方法传递匿名参数点击事件所处的类
    }




    //static防止内存泄露时，需要通过构造方法传递变量解决读取tv_result的问题
     static class  MyClickListener implements View.OnClickListener {

         private final View tv_result;

         public MyClickListener(View tv_result) {
             this.tv_result=tv_result;
         }

         @Override
        public void onClick(View view) {
            String nowTime = DateUtil.getNowTime();
            //获取结果
            String desc=String.format("%s 您点击了按钮：%s",nowTime,((Button)view).getText());
            ((TextView)tv_result).setText(desc);
        }
    }






    public void doClick(View view) {
        String nowTime = DateUtil.getNowTime();
        //获取结果
        String desc=String.format("%s 您点击了按钮：%s",nowTime,((Button)view).getText());
        ((TextView)tv_result).setText(desc);

    }
}